<?php
	header("Content-type: text/html; charset=utf-8");
	class Route{
		public $arguments = array();
		public $params = array('site','index');
		function __construct(){
			$this->Route();
		}
		
		function Route(){
			if(isset($_GET['url']) && $_GET['url']!=''){
				$params = explode('/',$_GET['url']);
				$params[0] = isset($params[0]) && !empty($params[0]) ? $params[0] : $this->params[0];
				$params[1] = isset($params[1]) && !empty($params[1]) ? $params[1] : $this->params[1];
			}else{
				$params = $this->params;
			}
			$this->params = $params;
		}
	}
	
	function require_element($name, $data=array()){
		ob_start();
		require_once('project/view/elements/'.$name.'.php');
		$element = ob_get_contents();
		ob_end_clean();
		echo $element;
	}
	
	class Application{
		public $controllerId;
		public $actionId;
		
		function __construct(){
			$route = new Route();
			$this->controllerId = $route->params[0];
			$this->actionId = $route->params[1];
		}
		
		function filterScriptPath($html){
			$search = '/project';
			$pattern = '/<script.+?src=[\"\'](.+?)[\"\'].*?>(.*?)<\/script>/i';
			if(preg_match_all($pattern, $html, $matches)){
				foreach($matches[0] as $n=>$v){
					if(isset($matches[1][$n])&&!empty($matches[1][$n])){
						$file = __dir__.str_replace($search, '', $matches[1][$n]);
						if(isset($file)&&!empty($file)&&file_exists($file)){
							$html = str_replace($matches[0][$n], '<script>'.file_get_contents($file).'</script>', $html);
						}
					}
				}		
			}
			$html = str_replace($search, '', $html);
			return $html;
		}
		
		function filterCssPath($html){
			$search = '/project';
			$pattern = '/<link.+?href=[\"\'](.*?)[\"\'].*?>/';
			if(preg_match_all($pattern, $html, $matches)){
				foreach($matches[0] as $n=>$v){
					if(isset($matches[1][$n])&&!empty($matches[1][$n])){
						$file = __dir__.str_replace($search, '', $matches[1][$n]);
						if(isset($file)&&!empty($file)&&file_exists($file)){
							$html = str_replace($matches[0][$n], '<style>'.file_get_contents($file).'</style>', $html);
						}
					}
				}
			}
			$html = str_replace($search, '', $html);
			return $html;
		}
		
		function getLayout(){
			$layout_file = "project/view/layouts/{$this->controllerId}.php";
			if(file_exists($layout_file)){
				ob_start();	
				require_once($layout_file);
				$layout = ob_get_contents();
				ob_end_clean();
			}else{
				exit($layout_file.'不存在！');
			}
			return $layout;
		}
		
		function getContent(){
			$content_file = "project/view/{$this->controllerId}/{$this->actionId}.php";
			if(file_exists($content_file)){
				ob_start();	
				require_once($content_file);
				$content = ob_get_contents();
				ob_end_clean();
			}else{
				exit($content_file.'不存在！');
			}
			return $content;
		}
		
		function render(){
			$layout = $this->getLayout();
			$content = $this->getContent();
			$html = str_replace('<!--{__CONTENT__}-->', $content, $layout);
			return $html;
		}
		
		function run(){
			$html = $this->render();
			echo $html;
			exit();
		}
	}
	
	$app = new Application();
	$app->run();
	
?>